##########################################
## Copyright 2012-2013 Ceruti Francesco & contributors
##
## This file is part of LiSP (Linux Show Player).
##########################################

from PyQt4 import QtCore
from PyQt4 import QtGui
from lisp.ui.preferences.preferences_widget import PreferencesWidget
import math

MIN_dB = 0.000000312  # -100dB


class Volume(PreferencesWidget):

    NAME = 'Volume'

    def __init__(self, size, parent=None):
        super(Volume, self).__init__(size, parent)

        self.verticalLayoutWidget = QtGui.QWidget(self)
        self.verticalLayoutWidget.setGeometry(0, 0, self.width(), 120)
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setMargin(0)

        self.groupBox = QtGui.QGroupBox(self.verticalLayoutWidget)

        self.gridLayout = QtGui.QGridLayout(self.groupBox)

        self.horizontalSlider = QtGui.QSlider(self.groupBox)
        self.horizontalSlider.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed))
        self.horizontalSlider.setMinimum(-1000)
        self.horizontalSlider.setMaximum(0)
        self.horizontalSlider.setPageStep(1)
        self.horizontalSlider.setProperty("value", 0)
        self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider.setTickPosition(QtGui.QSlider.TicksAbove)
        self.horizontalSlider.setTickInterval(100)
        self.gridLayout.addWidget(self.horizontalSlider, 0, 0)

        self.label = QtGui.QLabel(self.groupBox)
        self.label.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred))
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.gridLayout.addWidget(self.label, 0, 1)

        self.checkMute = QtGui.QCheckBox(self.groupBox)
        self.gridLayout.addWidget(self.checkMute, 1, 0)

        self.checkOver0 = QtGui.QCheckBox(self.groupBox)
        self.gridLayout.addWidget(self.checkOver0, 1, 1)
        self.checkOver0.toggled.connect(self.toggleGain)

        self.verticalLayout.addWidget(self.groupBox)

        self.horizontalSlider.valueChanged.connect(self.volumeChanged)

        self.retranslateUi()

    def retranslateUi(self):
        self.checkMute.setText("Mute")
        self.checkOver0.setText("Allow positive gain")
        self.groupBox.setTitle("Volume")
        self.label.setText("0.0 dB")

    def enableCheck(self, enable):
        self.groupBox.setCheckable(enable)
        self.groupBox.setChecked(False)

    def getConfiguration(self):
        conf = {}
        if(not (self.groupBox.isCheckable() and not self.groupBox.isChecked())):
            conf['volume'] = {'volume': math.pow(10, self.horizontalSlider.value() / 200), 'mute': self.checkMute.isChecked()}
        return conf

    def setConfiguration(self, conf):
        if(conf is not None):
            if('volume' in conf):
                if(type(conf['volume']) == type({})):
                    if('volume' in conf['volume']):
                        if(conf['volume']['volume'] > 1):
                            self.checkOver0.setChecked(True)
                        if(conf['volume']['volume'] == 0):
                            conf['volume']['volume'] = MIN_dB
                        self.horizontalSlider.setValue(int(200 * math.log10(conf['volume']['volume'])))
                    else:
                        self.horizontalSlider.setEnabled(False)
                    if('mute' in conf['volume']):
                        self.checkMute.setChecked(conf['volume']['mute'])

    def volumeChanged(self, value):
        txt = str(value / 10.0) + ' dB'
        if(value > 0):
            txt += ' (clipping)'
        self.label.setText(txt)

    def toggleGain(self, flag):
        if flag == True:
            self.horizontalSlider.setMaximum(200)
        else:
            self.horizontalSlider.setMaximum(0)
        #eventually show a label with warnings about clipping.
